# GF_PY3_CLASS_Python_3_OSPath_Operate.py
# Create by GF 2025-09-08 12:53

import os

# ##################################################

class Python_3_OSPath_Operate(object):

    # Cautions:
    # (1) 在 Windows 系统 + Python 3.12.0 中, os.listdir("D:") 返回的是当前工作目录在 D 盘下的内容, 而不是 D 盘根目录的内容。
    #     os.listdir("D:") 返回的是 D 盘当前工作目录的内容
    #     os.listdir("D:\\") 或 os.listdir("D:/") 才会返回 D 盘根目录的内容

    # Examples for Get Partition:
    # >>> OSPath = Python_3_OSPath_Operate()
    # >>>
    # >>> Result = OSPath.Get_Partition(OSPath = "C:", Separator = '\\')
    # >>> print(Result)
    # C:\\
    # >>> Result = OSPath.Get_Partition(OSPath = "C:\\Windows\\System32", Separator = '\\')
    # >>> print(Result)
    # C:\\
    # >>> Result = OSPath.Get_Partition(OSPath = "/home", Separator = '/')
    # >>> print(Result)
    # /home/
    # >>> Result = OSPath.Get_Partition(OSPath = "/home/goufeng/.cshrc", Separator = '/')
    # >>> print(Result)
    # /home/

    # Examples for Get Directory:
    # >>> OSPath = Python_3_OSPath_Operate()
    # >>>
    # >>> Result = OSPath.Get_Directory(OSPath = "C:", Separator = '\\')
    # >>> print(Result)
    # <Empty Character>
    # >>> Result = OSPath.Get_Directory(OSPath = "C:\\Windows\\System32", Separator = '\\')
    # >>> print(Result)
    # Windows\\System32
    # >>> Result = OSPath.Get_Directory(OSPath = "/home", Separator = '/')
    # >>> print(Result)
    # <Empty Character>
    # >>> Result = OSPath.Get_Directory(OSPath = "/home/goufeng/.cshrc", Separator = '/')
    # >>> print(Result)
    # goufeng/.cshrc

    # Examples for Combining Partition and Directory:
    # >>> OSPath = Python_3_OSPath_Operate()
    # >>>
    # >>> Result = OSPath.Combining_Partition_and_Directory(Partition = "C:", Directory = "\\Program Files", Separator = '\\')
    # >>> print(Result)
    # C:\\Program Files
    # >>> Result = OSPath.Combining_Partition_and_Directory(Partition = "C:", Directory = "Program Files", Separator = '\\')
    # >>> print(Result)
    # C:\\Program Files
    # >>> Result = OSPath.Combining_Partition_and_Directory(Partition = "home", Directory = "/goufeng", Separator = '/')
    # >>> print(Result)
    # /home/goufeng
    # >>> Result = OSPath.Combining_Partition_and_Directory(Partition = "home", Directory = "goufeng", Separator = '/')
    # >>> print(Result)
    # /home/goufeng

    # Examples for Backward:
    # >>> OSPath = Python_3_OSPath_Operate()
    # >>>
    # >>> Result = OSPath.Backward(OSPath = "C:\\Windows", Separator = '\\')
    # >>> print(Result)
    # C:\\
    # >>> Result = OSPath.Backward(OSPath = "C:\\Windows\\System32", Separator = '\\')
    # >>> print(Result)
    # C:\\Windows
    # >>> Result = OSPath.Backward(OSPath = "/home/goufeng", Separator = '/')
    # >>> print(Result)
    # /home/
    # >>> Result = OSPath.Backward(OSPath = "/home/goufeng/.cshrc", Separator = '/')
    # >>> print(Result)
    # /home/goufeng

    def Get_Partition(self, OSPath:str, Separator:str = os.sep) -> str:

        OSPath_Transform:str = OSPath
        # ..........................................
        if (OSPath_Transform[0] == '/'):
            # Purpose for Linux: "/home/username/.cshrc" => "home/username/.cshrc"
            OSPath_Transform = OSPath_Transform[1:len(OSPath_Transform)]
        # ..........................................
        OSPath_Split:list = OSPath_Transform.split(Separator)
        # ..........................................
        if (OSPath[0] == '/'):
            return Separator + OSPath_Split[0] + Separator
        # ..........................................
        if (OSPath[0] != '/' and ':' in OSPath_Split[0]):
            return OSPath_Split[0] + Separator

    def Get_Directory(self, OSPath:str, Separator:str = os.sep) -> str:

        OSPath_Transform:str = OSPath
        # ..........................................
        if (OSPath_Transform[0] == '/'):
            # Purpose for Linux: "/home/username/.cshrc" => "home/username/.cshrc"
            OSPath_Transform = OSPath_Transform[1:len(OSPath_Transform)]
        # ..........................................
        OSPath_Directory:str = OSPath
        # ..........................................
        OSPath_Part:str   = self.Get_Partition(OSPath = OSPath, Separator = Separator)
        OSPath_Part       = OSPath_Part.lstrip(Separator)
        OSPath_Part       = OSPath_Part.rstrip(Separator)
        OSPath_Split:list = OSPath_Transform.split(Separator)
        # ..........................................
        if (len(OSPath_Split) == 1 and OSPath_Split[0] == OSPath_Part):
            # Purpose for Windows: "C:" => str('')
            # Purpose for Linux: "/home" => str('')
            OSPath_Directory = str('')
        if (len(OSPath_Split) >= 2 and OSPath_Split[0] == OSPath_Part):
            # Purpose for Windows: "C:\\Windows\\System32" => "Windows\\System32"
            # Purpose for Linux: "/home/goufeng/.cshrc" => "goufeng/.cshrc"
            OSPath_Directory = Separator.join(OSPath_Split[1:len(OSPath_Split)])
        # ..........................................
        return OSPath_Directory

    def Combining_Partition_and_Directory(self, Partition:str, Directory:str, Separator:str = os.sep) -> str:

        Partition_Transform:str = Partition
        Directory_Transform:str = Directory
        Directory_Transform     = Directory.lstrip(Separator)
        Directory_Transform     = Directory.rstrip(Separator)
        # ..........................................
        return Partition_Transform + Directory_Transform

    def Backward(self, OSPath:str, Separator:str = os.sep) -> str:

        Partition:str = self.Get_Partition(OSPath = OSPath, Separator = Separator)
        Directory:str = self.Get_Directory(OSPath = OSPath, Separator = Separator)
        # ..........................................
        if (Directory == str('')):
            return OSPath
        # ..........................................
        if (Directory != str('')):
            if (Directory[0] == Separator):
                # Purpose for Windows: "\\Windows\\System32" => "Windows\\System32"
                # Purpose for Linux: "/goufeng/.cshrc" => "goufeng/.cshrc"
                Directory = Directory[1:len(Directory)]
            # ......................................
            Directory_Split:list = Directory.split(Separator)
            Directory_Split      = Directory_Split[0:len(Directory_Split) - 1]
            Directory            = Separator.join(Directory_Split)
            # ......................................
            return self.Combining_Partition_and_Directory(Partition = Partition, Directory = Directory, Separator = Separator)

# Signed by GF.
